module Fighter {
    /**
     * 对战敌方防守场景 ---刘伟
     * */
    export class EnemyDefense extends egret.DisplayObjectContainer {
        constructor() {
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);
            this.Timer_fire.addEventListener(egret.TimerEvent.TIMER, this.fire, this);
            this.flyManager = new FlyManager(this);
            this.BulletManager = new Bullet.BulletManager(this);
            this.Animation = new Animation.AnimationManager(this);
        }
        /**开始制造飞机 */
        public static startCreatePlane = false;
        /**容器名 */
        public name: string = "EnemyDefense";
        /**接受全屏攻击 */
        public static screenAtk: boolean = false;
        /**动画管理类*/
        public Animation: Animation.AnimationManager;
        /**飞机层 */
        private flyManager: FlyManager;
        /**子弹层 */
        public BulletManager: Bullet.BulletManager;
        /**开始判断是否结束*/
        public static Is_Over: boolean = false;
        /**发射子弹间隔*/
        private Timer_fire: egret.Timer = new egret.Timer(500, 0);
        /**子弹发射角度 */
        public angle: number;
        /**上一帧时间点 */
        public lastTimer: number;
        public is_game_Win: boolean = false;
        public addToStage() {

            if (GameUtil.PlayerMessage.instance.onePerson) {
                this.flyManager.fightRoundStart(GameUtil.PlayerMessage.instance.Aidefense);
                EnemyDefense.Is_Over = true;
                this.Timer_fire.start();
            }

            //每帧监听
            this.addEventListener(egret.Event.ENTER_FRAME, this.Update, this);
        }

        /**场景被移除出舞台时运行 */
        private removeFromStage() {

        }

        /**每帧运行*/
        public Update() {
            //计算每帧时间间隔
            let now = egret.getTimer();
            let deltaTime = now - this.lastTimer;
            this.lastTimer = now;

            if (!GameUtil.PlayerMessage.instance.onePerson && EnemyDefense.startCreatePlane) {
                EnemyDefense.startCreatePlane = false;
                this.flyManager.fightRoundStart(GameUtil.PlayerMessage.instance.EneMissList);
                this.Timer_fire.start();
            }

            this.flyManager.update(deltaTime);
            this.BulletManager.update(deltaTime);
            this.batteryAngle();
            if (EnemyDefense.Is_Over) {
                GameUtil.PlayerMessage.instance.is_emyTurryAtk=false;
                this.enemyDefenseEnd();
            }
            
            this.isGameOver();
        }
        /**炮台发射子弹 */
        private fire() {
            let point = new egret.Point(GameUtil.PlayerMessage.instance.HomePoint.x, GameUtil.PlayerMessage.instance.HomePoint.y);
            this.BulletManager.createBullet(1, point, this.angle);
            //正在攻击
            GameUtil.PlayerMessage.instance.is_emyTurryAtk=true;
        }
        /**确定基地炮台对向第一架敌机的角度*/
        public batteryAngle() {
            if (this.flyManager.productflys.length > 0) {
                let plane = this.flyManager.productflys[0].fly;
                let angle =
                    Math.atan((plane.x - GameUtil.PlayerMessage.instance.HomePoint.x)
                        / (GameUtil.PlayerMessage.instance.HomePoint.y - plane.y));
                this.angle = angle;
            }
            else {
                this.angle = 0;
            }
            EnemyDefenseUI.EnemyHomeAngle = this.angle;
        }
        /**场景结束 */
        private enemyDefenseEnd() {
            if (this.flyManager.productflys.length == 0) {
                this.Timer_fire.reset();
                this.BulletManager.fightRoundEnd();
                this.removeEventListener(egret.Event.ENTER_FRAME, this.Update, this);
                EnemyDefense.Is_Over = false;
                if (GameUtil.PlayerMessage.instance.onePerson)
                    PrepareUI.isReadyShow = true;
            }
        }
        /**判断游戏是否胜利 */
        private isGameOver() {
            if (GameUtil.PlayerMessage.instance.isGameOver) {
                this.removeChildren();
                this.flyManager.fightRoundEnd();
            }
            if (GameUtil.PlayerMessage.instance.enemyHomeHp <= 0) {
                GameUtil.PlayerMessage.instance.isGameOver = true;
                this.Timer_fire.reset();
                this.BulletManager.fightRoundEnd();
                this.flyManager.fightRoundEnd();
                this.removeEventListener(egret.Event.ENTER_FRAME, this.Update, this);
                this.removeChildren();
                this.is_game_Win = true;
                GameUtil.PlayerMessage.instance.getEnemy = [];
                GameUtil.PlayerMessage.instance.Game2addEnemy=[];
                this.flyManager.productflys=[];
                if (GameUtil.PlayerMessage.instance.onePerson) {
                    //GameUtil.PlayerMessage.instance.getEnemy = [];
                    for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                        GameUtil.PlayerMessage.instance.getEnemy.push(EnemyType["feiji0_png"]);
                    }
                    Ai.AiBool = false;
                }
            }
        }
    }
}